package com.shiliao.app.activity.index.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.LinearLayout;

import com.shiliao.app.R;
import com.shiliao.app.activity.MainActivity;
import com.shiliao.app.activity.index.adapter.MyAdapter;
import com.shiliao.app.activity.index.presenter.IndexPresenter;
import com.shiliao.app.activity.index.view.MainIndexView;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.FileNameManage;
import com.shiliao.app.utils.PermissionsChecker;
import com.yimei.framework.base.ui.BaseFragment;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.PreferenceHelper;
import com.yimei.framework.util.SystemUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yinzhiqiang on 2017/9/6.
 */

public class IndexFragment extends BaseFragment implements MainIndexView,
        MainActivity.IPageChangedListener {
    private List<BaseFragment> fragmentList;
    private List<String> stringList = new ArrayList<>();

    @BindView(R.id.viewpager_ShouYe)
    ViewPager mViewPager;

    @BindView(R.id.mTabLayout)
    TabLayout mTabLayout;

    @BindView(R.id.index_search)
    LinearLayout search;
    private MyAdapter myAdapter;

    private IndexPresenter indexPresenter;

    private String auditor;

    @Override
    public int getLayoutId() {
        return R.layout.activity_index;
    }

    @Override
    public void onPageChanged(int position) {
        if (MainActivity.getCurrentView() == MainActivity.POSITION_INDEX) {
            if (getView() == null && getActivity() != null) {
                mContext = getActivity();
                initFragment();
            } else if (!isInit
                    && getView() != null) {
                //    initFragment();
            } else if (isInit && getView() != null) {
                //     requestData();
                //    initFragment();
            }
        }
    }

    @OnClick(R.id.index_search)
    public void search() {
        Intent intent = new Intent(getContext(), SearchActivity.class);
        intent.putExtra("from", "index");
        toActivity(intent);
    }

    @Override
    protected void initFragmentView() {
        if (!isInit && MainActivity.getCurrentView() == MainActivity.POSITION_INDEX) {
            LogUtil.e("yzq", "初始化...");
            initFragment();
        }
    }

    private void initFragment() {
        isInit = true;
        auditor= PreferenceHelper.readString(getContext(), FileNameManage.WELCOME_PAGE,
                FileNameManage.WELCOME_PAGE_IS_AUDITOR,"no");
        initdate();
        indexPresenter = new IndexPresenter(this);

//        indexPresenter.request(SystemUtils.getDeviceUni(getContext()),SystemUtils.getNativePhoneNumber(getContext()));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean hasPermission = PermissionsChecker.getInstance().lacksPermission(getActivity(),
                    Manifest.permission.READ_PHONE_STATE);
            if (hasPermission) {
                LogUtil.e("yzq", "请求权限:" + Manifest.permission.READ_PHONE_STATE);
                if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                        Manifest.permission.READ_PHONE_STATE)) {
                    // 用户拒绝授权
                    LogUtil.e("yzq", "用户拒绝授权=======");

                    return;
                }
                ActivityCompat.requestPermissions(getActivity(),
                        new String[]{Manifest.permission.READ_PHONE_STATE}, 0);
            } else {
                indexPresenter.request(SystemUtils.getDeviceUni(getContext()), SystemUtils.getNativePhoneNumber(getContext()));

            }
        } else {
            indexPresenter.request(SystemUtils.getDeviceUni(getContext()), SystemUtils.getNativePhoneNumber(getContext()));
        }
    }

    private void initdate() {
        fragmentList = new ArrayList<BaseFragment>();
        stringList.add("每日知识");
        stringList.add("我的收藏");
        stringList.add("日常提醒");
        if (!TextUtils.isEmpty(auditor)&&auditor.equals("yes")) {
            stringList.add("待审核");
        }
        fragmentList.add(new DailyKnowledgeFragment());
        fragmentList.add(new MyFavoritesFragment());
        fragmentList.add(new DailyRemindersFragment());
        if (!TextUtils.isEmpty(auditor)&&auditor.equals("yes")){
            fragmentList.add(new WaitExamineFragment());
        }
        myAdapter = new MyAdapter(getActivity().getSupportFragmentManager(), fragmentList, stringList);
        mViewPager.setAdapter(myAdapter);
        //设置预加载
        // mViewPager.setOffscreenPageLimit(3);
        //绑定ViewPager
        mTabLayout.setupWithViewPager(mViewPager);

        //TabLayout绑定adapter
        mTabLayout.setTabsFromPagerAdapter(myAdapter);
    }

    @Override
    public void saveUserId(String userId) {
        YMUserManager.saveUserId(getContext(), userId);
    }

    @Override
    public String getUserId() {
        return YMUserManager.getUserId(getContext());
    }

}
